﻿/* ==============================================================================
 * 功能描述：ModuleManager  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/4 11:29:29
 * ==============================================================================*/

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace ZZQFrameWork
{
    public class ModuleManager : Singleton<ModuleManager>
    {
        /// <summary>
        /// 已注册模块集合
        /// </summary>
        private Dictionary<string, BaseModule> moduleDic = null;

        protected override void Init()
        {
            moduleDic = new Dictionary<string, BaseModule>();
        }

        #region 从模块集合中获取对应模块

        /// <summary>
        /// 通过key在module集合中获取basemodule
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public BaseModule Get(string key)
        {
            if (moduleDic.ContainsKey(key))
            {
                return moduleDic[key];
            }
            return null;
        }

        public T Get<T>() where T : BaseModule
        {
            Type type = typeof (T);
            BaseModule module = Get(type.ToString());
            return module as T;
        }

        #endregion

        #region 注册加载模块

        /// <summary>
        /// 注册所有模块
        /// </summary>
        public void RegisterAllModule()
        {
            LoadModule(typeof(CharacterModule));
            LoadModule(typeof(BackpackModule));
            LoadModule(typeof(SkillModule));
            LoadModule(typeof(BattleModule));
        }

        /// <summary>
        /// 加载模块
        /// </summary>
        /// <param name="moduleType"></param>
        public void LoadModule(Type moduleType)
        {
            BaseModule module = Activator.CreateInstance(moduleType) as BaseModule;
            module.Load();
        }

        /// <summary>
        /// 注册模块
        /// </summary>
        /// <param name="module"></param>
        public void RegisterModule(BaseModule module)
        {
            Type t = module.GetType();
            RegisterModule(t.ToString(), module);
        }

        /// <summary>
        /// 注册模块
        /// </summary>
        /// <param name="key"></param>
        /// <param name="module"></param>
        public void RegisterModule(string key, BaseModule module)
        {
            if (!moduleDic.ContainsKey(key))
            {
                moduleDic.Add(key, module);
            }
        }

        #endregion

        #region 注销释放模块

        /// <summary>
        /// 注销模块
        /// </summary>
        /// <param name="module"></param>
        public void UnRegisterModule(BaseModule module)
        {
            Type t = module.GetType();
            UnRegisterModule(t.ToString());
        }

        /// <summary>
        /// 注销模块
        /// </summary>
        /// <param name="key"></param>
        public void UnRegisterModule(string key)
        {
            if (moduleDic.ContainsKey(key))
            {
                moduleDic[key].Release();
                moduleDic.Remove(key);
            }
        }

        /// <summary>
        /// 注销所有模块
        /// </summary>
        public void UnRegisterAllModule()
        {
            foreach (KeyValuePair<string, BaseModule> keyValuePair in moduleDic)
            {
                UnRegisterModule(keyValuePair.Key);
            }
            moduleDic.Clear();
        }

        #endregion

    }
}